/*
package io.renren.modules.oatools.utils.http;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.*;
import java.io.IOException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;

public class HttpClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    */
/**
     * http请求第三方接口
     *
     * @param url
     * @param param
     * @param headers
     * @return
     *//*

    public static String httpPost(String url, JSONObject param, Headers headers) {
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(getSSLSocketFactory(), getX509TrustManager())
                .hostnameVerifier(getHostnameVerifier())
                .build();

        String jsonString = param.toJSONString();
        logger.info("请求第三方接口,请求地址: {},请求体={}", url, jsonString);

        RequestBody body = RequestBody.create(jsonString, MediaType.parse("application/json; charset=utf-8"));
        Request.Builder builder = new Request.Builder()
                .url(url)
                .method("POST", body);
        if (null != headers) {
            builder.headers(headers);
        }

        Request request = builder.build();
        try (Response response = client.newCall(request).execute()) {
            // 检查响应
            if (!response.isSuccessful()) {
                throw new IOException("服务器响应失败, Unexpected code :" + response + "\n url:" + url);
            }

            int code = response.code();
            String result = response.body().string();

            // 输出响应体
            logger.info("请求第三方接口,请求地址: {},响应代码={},响应体={}", url, param, result);

            if (code != 200) {
                throw new IOException("服务器请求异常, Unexpected code :" + response + "\n url:" + url);
            }

            return result;
        } catch (IOException e) {
            logger.error("请求第三方接口,请求地址: {},请求体={}", url, jsonString, e);
            return null;
        }
    }

    public static String httpGet(String url, JSONObject param, Headers headers) {
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(getSSLSocketFactory(), getX509TrustManager())
                .hostnameVerifier(getHostnameVerifier())
                .build();

        String jsonString = param.toJSONString();
        logger.info("请求第三方接口,请求地址: {},请求体={}", url, jsonString);

        Request.Builder builder = new Request.Builder()
                .url(url)
                .method("GET", null);
        if (null != headers) {
            builder.headers(headers);
        }

        Request request = builder.build();
        try (Response response = client.newCall(request).execute()) {
            // 检查响应
            if (!response.isSuccessful()) {
                throw new IOException("服务器响应失败, Unexpected code :" + response + "\n url:" + url);
            }

            int code = response.code();
            String result = response.body().string();

            // 输出响应体
            logger.info("请求第三方接口,请求地址: {},响应代码={},响应体={}", url, param, result);

            if (code != 200) {
                throw new IOException("服务器请求异常, Unexpected code :" + response + "\n url:" + url);
            }

            return result;
        } catch (IOException e) {
            logger.error("请求第三方接口,请求地址: {},请求体={}", url, jsonString, e);
            return null;
        }
    }


    //获取这个SSLSocketFactory
    public static SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, getTrustManager(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception e) {
            logger.error("获取SSLSocketFactory失败", e);
            return null;
        }
    }

    //获取TrustManager
    private static TrustManager[] getTrustManager() {
        return new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
    }

    //获取HostnameVerifier
    public static HostnameVerifier getHostnameVerifier() {
        return (s, sslSession) -> true;
    }

    public static X509TrustManager getX509TrustManager() {
        X509TrustManager trustManager = null;
        try {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init((KeyStore) null);
            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
            if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
                throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
            }
            trustManager = (X509TrustManager) trustManagers[0];
        } catch (Exception e) {
            logger.error("获取X509TrustManager失败", e);
        }

        return trustManager;
    }

    public static boolean isAccessByIp(String serverName) {
        // 检查serverName是否是IP地址
        return "localhost".equals(serverName) || serverName.matches("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$") ||
                serverName.contains(":"); // IPv6包含冒号
    }
}*/